package com.zy.mc_module.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.content.ContextCompat;
import androidx.appcompat.content.res.AppCompatResources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Space;
import android.widget.TextView;

import com.zy.mc_module.R;

/**
 * Created on 18/10/31
 * 封装头部View
 */
public class CustomHeadLayout extends RelativeLayout {
    private Context context;
    private onLeftClick mLeftClick;
    private onRightClick1 mRightClick1;
    private onRightClick2 mRightClick2;
    private onRightClick3 mRightClick3;

    private Space space;
    private TextView pointText;
    private View lineView, pointView;
    private ImageButton itemLeftImage;
    private RelativeLayout relativeLayout;//带红点的图标
    private RelativeLayout relativeLayoutWrap;
    private TextView itemTextView, textRight;
    private ImageButton itemRightImage1, itemRightImage2;

    private float rightsize;
    private boolean isChecked = false;//img2的选中状态

    private int pointType;
    private String text, text_right;
    private boolean textVis, textRightVis, isStatusBar, lineVis;
    private int color, textColor, rightColor, pointText_c;//图标颜色,中间文字颜色,右边文字颜色
    private int pointTextDrawable, pointViewDrawable;

    private Drawable drawableLeft, drawableRight1, drawableRight2;

    public CustomHeadLayout(Context context) {
        this(context, null);
        this.context = context;
    }

    public CustomHeadLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public CustomHeadLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initLayout(context);
        initAttr(context, attrs);
        initListener();
    }

    private void initListener() {
        itemLeftImage.setOnClickListener(mOnClickListener);
        itemRightImage1.setOnClickListener(mOnClickListener);
        itemRightImage2.setOnClickListener(mOnClickListener);
        textRight.setOnClickListener(mOnClickListener);
    }

    private void initLayout(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_head_linear, this);
        this.pointView = findViewById(R.id.customHead_layout_point_view);
        this.pointText = findViewById(R.id.customHead_layout_point_text);
        this.space = findViewById(R.id.customHead_layout_space);
        this.lineView = findViewById(R.id.customHead_layout_line);
        this.itemLeftImage = findViewById(R.id.customHead_layout_left_img);
        this.itemTextView = findViewById(R.id.customHead_layout_title_text);
        this.textRight = findViewById(R.id.customHead_layout_title_right);
        this.itemRightImage1 = findViewById(R.id.customHead_layout_right_img1);
        this.itemRightImage2 = findViewById(R.id.customHead_layout_right_img2);
        this.relativeLayout = findViewById(R.id.customHead_layout_relative);
        this.relativeLayoutWrap = findViewById(R.id.custom_head_rl);
    }

    private void initAttr(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomHeadLayout);

            pointType = ta.getInt(R.styleable.CustomHeadLayout_Head_Point, 2);//0文本/1红点/2不显示
            pointText_c = ta.getInt(R.styleable.CustomHeadLayout_Head_point_textColor, 0xffffffff);
            pointTextDrawable = ta.getResourceId(R.styleable
                    .CustomHeadLayout_Head_point_text_drawable, R.drawable.circle_no_red_bg);
            pointViewDrawable = ta.getResourceId(R.styleable
                    .CustomHeadLayout_Head_point_view_drawable, R.drawable.circle_no_red_bg);

            lineVis = ta.getBoolean(R.styleable.CustomHeadLayout_Head_lineVis, false);
            isStatusBar = ta.getBoolean(R.styleable.CustomHeadLayout_Head_Statusbar, false);

            text = ta.getString(R.styleable.CustomHeadLayout_Head_text);
            textVis = ta.getBoolean(R.styleable.CustomHeadLayout_Head_textVis, true);
            textColor = ta.getInt(R.styleable.CustomHeadLayout_Head_textColor, 0xffffffff);

            text_right = ta.getString(R.styleable.CustomHeadLayout_Head_textRight);
            textRightVis = ta.getBoolean(R.styleable.CustomHeadLayout_Head_textRightVis, true);
            rightColor = ta.getInt(R.styleable.CustomHeadLayout_Head_textRightColor, 0xff000000);
            rightsize = ta.getDimension(R.styleable.CustomHeadLayout_Head_textRightSize, sp2px(17f));


            //兼容矢量图
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.0以上
                drawableLeft = ta.getDrawable(R.styleable.CustomHeadLayout_Head_drawableLeft);
                drawableRight1 = ta.getDrawable(R.styleable.CustomHeadLayout_Head_drawableRight1);
                drawableRight2 = ta.getDrawable(R.styleable.CustomHeadLayout_Head_drawableRight2);
            } else {
                //5.0以下
                int leftImg_Id = ta.getResourceId(R.styleable.CustomHeadLayout_Head_drawableLeft, -1);
                int right1Img_Id = ta.getResourceId(R.styleable.CustomHeadLayout_Head_drawableRight1, -1);
                int right2Img_Id = ta.getResourceId(R.styleable.CustomHeadLayout_Head_drawableRight2, -1);
                if (leftImg_Id != -1) {
                    drawableLeft = AppCompatResources.getDrawable(getContext(), leftImg_Id);
                }
                if (right1Img_Id != -1) {
                    drawableRight1 = AppCompatResources.getDrawable(getContext(), right1Img_Id);
                }
                if (right2Img_Id != -1) {
                    drawableRight2 = AppCompatResources.getDrawable(getContext(), right2Img_Id);
                }
            }
            boolean leftVis = ta.getBoolean(R.styleable.CustomHeadLayout_Head_drawableLeftVis, true);
            boolean rightVis1 = ta.getBoolean(R.styleable.CustomHeadLayout_Head_drawableRightVis1, true);
            boolean rightVis2 = ta.getBoolean(R.styleable.CustomHeadLayout_Head_drawableRightVis2, true);

            //图标颜色
            int drawableColor = ta.getInt(R.styleable.CustomHeadLayout_Head_drawableColor, 0xff444444);
            ta.recycle();

            //是否启用红点图标
            if (pointType == 0) {
                //文本
                pointView.setVisibility(GONE);
                pointText.setVisibility(VISIBLE);
                pointText.setTextColor(pointText_c);
                pointText.setBackgroundResource(pointTextDrawable);
            } else if (pointType == 1) {
                //红点
                pointText.setVisibility(GONE);
                pointView.setVisibility(VISIBLE);
                pointView.setBackgroundResource(pointViewDrawable);
            } else {
                pointView.setVisibility(GONE);
                pointText.setVisibility(GONE);
            }

            //标题栏底部是否显示分割线
            if (null != lineView) {
                if (lineVis) {
                    lineView.setVisibility(VISIBLE);
                } else {
                    lineView.setVisibility(GONE);
                }
            }

            //是否设置状态栏高度
            if (null != space && isStatusBar) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    int height = getStatusBarHeight(context);
                    ViewGroup.LayoutParams params = space.getLayoutParams();
                    params.height = height;
                    space.setLayoutParams(params);
                }
            }

            //标题
            if (text != null && itemTextView != null) {
                if (textVis) {
                    itemTextView.setText(text);
                    itemTextView.setTextColor(textColor);
                    itemTextView.setVisibility(VISIBLE);
                } else {
                    itemTextView.setVisibility(GONE);
                }
            }
            //右边文字
            if (text_right != null && textRight != null) {
                textRight.setText(text_right);
                textRight.setTextColor(rightColor);
                textRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightsize);
                if (textRightVis) {
                    textRight.setVisibility(VISIBLE);
                } else {
                    textRight.setVisibility(GONE);
                }
            }
            //左边图标
            if (drawableLeft != null && itemLeftImage != null) {
                itemLeftImage.setImageDrawable(drawableLeft);
                itemLeftImage.setColorFilter(drawableColor);
                if (leftVis) {
                    itemLeftImage.setVisibility(VISIBLE);
                } else {
                    itemLeftImage.setVisibility(GONE);
                }
            }
            //最右边图标
            if (drawableRight1 != null && itemRightImage1 != null) {
                itemRightImage1.setColorFilter(drawableColor);
                itemRightImage1.setImageDrawable(drawableRight1);
                if (rightVis1) {
                    relativeLayout.setVisibility(VISIBLE);
                    itemRightImage1.setVisibility(VISIBLE);
                } else {
                    relativeLayout.setVisibility(GONE);
                    itemRightImage1.setVisibility(GONE);
                }
            }
            //最右边 第二个图标
            if (drawableRight2 != null && itemRightImage2 != null) {
                itemRightImage2.setColorFilter(drawableColor);
                itemRightImage2.setImageDrawable(drawableRight2);
                if (rightVis2) {
                    itemRightImage2.setVisibility(VISIBLE);
                    //todo img2颜色不和其他两个img一起设置
                    //itemRightImage2.setColorFilter(drawableColor);
                } else {
                    itemRightImage2.setVisibility(GONE);
                }
            }
        }
    }

    public void setLeftImageDrawable(int DrawableId) {
        if (itemLeftImage != null) {
            itemLeftImage.setImageDrawable(ContextCompat.getDrawable(getContext(), DrawableId));
        }
    }

    public void setRightImage1Drawable(int DrawableId) {
        if (itemRightImage1 != null) {
            itemRightImage1.setImageDrawable(ContextCompat.getDrawable(getContext(), DrawableId));
        }
    }

    /***
     * img2 一般为收藏/点赞 ,设置选中状态
     * @param drawableId
     * @param checked
     */
    public void setRightImage2Drawable(int drawableId, boolean checked) {
        if (null != itemRightImage2) {
            itemRightImage2.setImageDrawable(ContextCompat.getDrawable(getContext(), drawableId));
            isChecked = checked;
            if (checked) {
                //选中,去掉设置的颜色
                itemRightImage2.clearColorFilter();
            } else {
                itemRightImage2.setColorFilter(color);
            }
        }
    }

    public boolean getChecked() {
        return isChecked;
    }

    public void setText(String str) {
        if (itemTextView != null) {
            itemTextView.setText(str);
        }
    }

    public void setRightText(String str) {
        if (textRight != null) {
            textRight.setText(str);
        }
    }

    public void setTextColor(int color) {
        if (itemTextView != null) {
            itemTextView.setTextColor(color);
        }
    }

    public void setDrawableColor(int color) {
        if (itemLeftImage != null) {
            itemLeftImage.setColorFilter(color);
        }
        if (itemRightImage1 != null) {
            itemRightImage1.setColorFilter(color);
        }
        if (itemRightImage2 != null && !isChecked) {
            itemRightImage2.setColorFilter(color);
        }
        this.color = color;
    }

    public void getText() {
        if (itemTextView != null) {
            itemTextView.getText();
        }
    }

    public void setLeftVis(boolean isVisible) {
        if (drawableLeft != null) {
            if (isVisible) {
                itemLeftImage.setVisibility(VISIBLE);
            } else {
                itemLeftImage.setVisibility(GONE);
            }
        }
    }

    public void setRightVis(boolean isVisible) {
        if (textRight != null) {
            if (isVisible) {
                textRight.setText(text_right);
                textRight.setTextColor(rightColor);
                textRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightsize);
                textRight.setVisibility(VISIBLE);
            } else {
                textRight.setVisibility(GONE);
            }
        }
    }

    public void setPoint(Object object) {
        if (object instanceof Boolean) {
            //view
            if (pointView != null) {
                boolean b = (boolean) object;
                pointView.setVisibility(b ? VISIBLE : GONE);
            }
        } else if (object instanceof String) {
            //text
            if (pointText != null) {
                int num = Integer.parseInt((String) object);
                if (num <= 0) {
                    pointText.setVisibility(GONE);
                } else {
                    pointText.setVisibility(VISIBLE);
                    pointText.setText((String) object);
                }
            }
        }
    }

    public View getRightImg1() {
        return itemRightImage1;
    }

    public void showNone() {
        //不显示title
        relativeLayoutWrap.setVisibility(GONE);
    }

    public void setLeftClickListener(onLeftClick leftClickListener) {
        mLeftClick = leftClickListener;
    }

    public void setRightClickListener1(onRightClick1 rightClickListener) {
        mRightClick1 = rightClickListener;
    }

    public void setRightClickListener2(onRightClick2 rightClickListener) {
        mRightClick2 = rightClickListener;
    }

    public void setRightClickListener3(onRightClick3 rightClickListener) {
        mRightClick3 = rightClickListener;
    }

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.customHead_layout_left_img:
                    if (mLeftClick != null) {
                        mLeftClick.leftClick(v);
                    }
                    break;
                case R.id.customHead_layout_right_img1:
                    if (mRightClick1 != null) {
                        mRightClick1.rightClick1(v);
                    }
                    break;
                case R.id.customHead_layout_right_img2:
                    if (mRightClick2 != null) {
                        mRightClick2.rightClick2(v);
                    }
                    break;
                case R.id.customHead_layout_title_right:
                    if (mRightClick3 != null) {
                        mRightClick3.rightClick3(v);
                    }
                    break;
            }
        }
    };

    public interface onLeftClick {
        void leftClick(View view);
    }

    public interface onRightClick1 {
        void rightClick1(View view);
    }

    public interface onRightClick2 {
        void rightClick2(View view);
    }

    public interface onRightClick3 {
        void rightClick3(View view);
    }

    protected int sp2px(float sp) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * scale + 0.5f);
    }

    /**
     * 获取手机状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
